package com.netcracker.studportal.server.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.netcracker.studportal.server.dao.ExamDAO;
import com.netcracker.studportal.server.model.Exam;
import com.netcracker.studportal.server.model.Professor;
import com.netcracker.studportal.server.util.HibernateUtil;

public class ExamDAOImpl implements ExamDAO {
	private static final Logger LOG = Logger.getLogger(ExamDAOImpl.class);
	
	@Override
	public void Add(Exam exam) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(exam);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}

	}

	@Override
	public void Update(Exam exam) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(exam);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Exam getById(Long id) throws Exception {
		Session session = null;
		Exam ex = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ex = (Exam) session.load(Exam.class, id);
			Hibernate.initialize(ex.getProfessor());
			Hibernate.initialize(ex.getSubject());
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return ex;
	}

	@Override
	public void Delete(Exam exam) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(exam);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}

	}

	@Override
	public Collection<Exam> getAll() throws Exception {
		Session session = null;
		List<Exam> exam = new ArrayList<Exam>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			exam = session.createCriteria(Exam.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return exam;
	}

	@Override
	public Collection<Exam> getExamByProf(Professor prof) throws Exception {
		// TODO Auto-generated method stub
		Session session = null;
		List<Exam> exam_list;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			exam_list = (List<Exam>) session
					.createSQLQuery(
							"select * from exams where prof_id=" + prof.getId())
					.addEntity(Exam.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			//TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return exam_list;
	}

	@Override
	public Collection<Exam> getExamBySubject(Long subject_id) throws Exception {
		// TODO Auto-generated method stub
				Session session = null;
				List<Exam> exam_list;
				try {
					session = HibernateUtil.getSessionFactory().openSession();
					exam_list = (List<Exam>) session
							.createSQLQuery(
									"select * from exams where subject_id=" + subject_id)
							.addEntity(Exam.class).list();
				} catch (Exception e) {
					LOG.error("Error: ", e);
					throw new Exception(e);
				} finally {
					if (session != null && session.isOpen()) {
						session.close();
					}
				}
				return exam_list;
	}
	
	/**
	 * Realization of singleton.
	 */
	private ExamDAOImpl() {}
	private static ExamDAO instance;
	public static ExamDAO getInstance() {
		if (instance == null) {
			instance = new ExamDAOImpl();
		}
		return instance;
	}
	
}
